<template>
  <section class="apply">
    <div>
      <el-avatar :size="46" :src="row?.portrait" />
      <div class="msg_info" v-if="params === '-2'">
        <span class="msg_name">
          <i>
            {{ row?.nickName }}
          </i>
        </span>
        <span v-if="row?.type">
          <i class="v_ms" v-if="(row?.type === '2' && row?.applyStatus === '0')">[好友验证]</i>
          <i class="text">{{ row?.reason }}</i>
        </span>
      </div>

      <div class="msg_info" v-if="params === '-1'">
        <span class="msg_name">
          <i v-if="row?.invitationName">
            {{ row?.invitationName }}
            &nbsp;
            邀请
            &nbsp;
          </i>
          <i style="color: #2657B5;">
            {{ row?.nickName }}
            &nbsp;
          </i>
          <i>
            加入{{ row?.groupName }}
          </i>
        </span>
        <span>
          <i class="text">{{ row?.reason }}</i>
        </span>
      </div>
      <div class="action_btn">
        <button v-if="state.canClick && params === '-1'" class="refuse" @click="refuseAction(row.applyId)">拒绝</button>
        <button :class="!state.canClick ? 'otherMsg' : 'submit'" @click="applyFriend">{{ state?.text }}</button>
      </div>
    </div>
  </section>
</template>
<script>
import { defineComponent, computed } from 'vue';
import { applyAgreeFriend, refuseApply } from '@/server/friend'
import { useRequest } from 'vue-hooks-plus';
import { useStore } from 'vuex';
export default defineComponent({
  name: "ApplyMsg",
  props: {
    params: "",
    row: {
      type: Object,
      default: () => ({})
    }
  },
  setup (props) {
    const store = useStore()
    const state = computed(() => {
      let { applyStatus, type } = props.row
      // 申请状态 0、等待验证 1、同意 2、拒绝 3、忽略 / 删除 4、过期
      // 类型 好友：1、我申请 2、向我申请 群：无
      if (type === '') {
        //群申请 只有向我申请
        type = '2'
      }
      const info = {
        "0": {
          text: type === '2' ? '同意' : '等待验证',
          canClick: type === '2'
        },
        "1": {
          text: '已同意',
          canClick: false
        },
        "2": {
          text: "拒绝",
          canClick: false,
        },
        "3": {
          text: "忽略/删除",
          canClick: false
        },
        "4": {
          text: '已过期',
          canClick: false
        }
      }
      return Reflect.get(info, applyStatus)
    })
    const { run: applyFriend } = useRequest(() => {
      if (state.value?.canClick) {
        return applyAgreeFriend(props?.row?.applyId)
      }
      return Promise.reject()
    }, {
      onSuccess: res => {
        store.dispatch('checkApplyList')
      },
      manual: true
    })

    const { run: refuseAction } = useRequest(refuseApply, {
      manual: true,
      onSuccess: res => {
        store.dispatch('checkApplyList')
      }
    })
    return {
      state,
      applyFriend,
      refuseAction
    }
  }
})
</script>

<style scoped lang="less">
.apply {
  width: 100%;
  padding: 0 20px;

  >div {
    display: flex;
    background-color: #fff;
    border: 1px solid #EEEEEE;
    background: #FFFFFF;
    border-radius: 12px;
    padding: 10px;
    align-items: center;

    .msg_info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 4px 0;
      margin-left: 10px;

      .msg_name {
        margin-bottom: 7px;
      }

      .v_ms {
        font-size: 14px;

        font-weight: 400;
        color: #F74C31;
        margin-right: 5px;
      }

      .text {
        font-size: 14px;

        font-weight: 400;
        color: #787878;

      }
    }


    .action_btn {
      margin-left: auto;

      .submit {

        outline: none;
        border: none;
        background: linear-gradient(356deg, #009BFD 8%, #00E8E8 100%);
        border-radius: 15px;
        font-size: 14px;

        font-weight: 500;
        color: #FFFFFF;
        width: 78px;
        height: 30px;
        cursor: pointer;
        letter-spacing: 2px;
      }

      .otherMsg {
        font-size: 16px;

        font-weight: 400;
        color: #787878;
        margin-left: auto;
        outline: none;
        border: none;
        pointer-events: none;
        background-color: transparent;
      }

      .refuse {
        width: 78px;
        height: 30px;
        border: none;
        font-weight: 500;
        cursor: pointer;
        font-size: 14px;
        border-radius: 15px;
        margin-right: 10px;
        letter-spacing: 2px;
      }
    }
  }
}
</style>
